home *** CD-ROM | disk | FTP | other *** search
- Path: status.gen.nz!sgazza
- From: gazza@iconz.co.nz (Gary Elmes)
- Newsgroups: comp.lang.c++
- Subject: Heap management in .DLLs - I think I'm confused
- Date: Fri, 12 Jan 96 04:49:20 GMT
- Organization: Internet Company of New Zealand
- Message-ID: <4d2ppb$acv@status.gen.nz>
- NNTP-Posting-Host: sgazza.internet.co.nz
- X-Newsreader: News Xpress Version 1.0 Beta #4
-
-
- I'm using Turbo C++ v3.1 to write a .DLL file (to be called from Lotus Notes
- as it happens, but that's by the by).
-
- The Borland doco on writing DLLs is a bit skimpy. Nevertheless, I plunged in,
- full of confidence, until I came across the following in some example Borland
- DLL source:
-
- >
- >// ObjectWindows - (C) Copyright 1992 by Borland International
- >
- ..
- >
- >int FAR PASCAL LibMain(HINSTANCE hInstance, WORD /*wDataSeg*/,
- > WORD /* cbHeapSize */, LPSTR lpCmdLine)
- >{
- > int TheStatus;
- >
- > // Note that we can't ensure that DLLHelloLib is allocated
- > // in memory owned by DLLHELLO.DLL. Hence, it will get owned
- > // by the first application that caused DLLHELLO.DLL to be
- > // loaded.
- > DLLHelloLib = new TModule("DLLHello", hInstance, lpCmdLine);
- > TheStatus = DLLHelloLib->Status;
- > if ( TheStatus != 0 )
- > {
- > delete DLLHelloLib;
- > DLLHelloLib = NULL;
- > }
- > return (TheStatus == 0);
- >}
- >
- >int FAR PASCAL WEP ( int /*bSystemExit*/ )
- >{
- > // don't delete DLLHelloLib here. We haven't guaranteed that
- > // DLLHelloLib points to memory owned by DLLHELLO.DLL. It
- > // will belong to the application that first caused DLLHELLO.DLL
- > // to be loaded. At the time WEP is called, that application,
- > // and all of its memory, has been deleted.
- > return 1;
- >}
- >
-
- At which point I got sweaty palms.
-
- Are the comments here trying to tell me that a .DLL doesn't have its own heap?
- That all objects created via "new" are added to the calling app's heap?
-
- I knew that .DLLs don't have their own stack, but no _heap_ ?!?!
-
- This presumably means that I have to make sure that any objects added to the
- heap when the DLL is invoked by appA are only referenced by appA, and are
- deleted before appA terminates.
-
- If this is the case, then why are Borland suggesting that we declare an OWL
- TModule object (that will typically be referenced during invocations from all
- calling apps) in the manner shown above - on the heap? Surely, the TModule
- object will get wiped out as soon as the original calling app terminates, even
- if another app is still using the .DLL.
-
- Or have I got it all wrong? Or have Borland got it all wrong? Have I been
- wasting my time putting a HEAPSIZE statement on my .DEF file all this time?
-
-
- Answers to all these questions, and more, would be very much appreciated.
-
- TIA
-
-
- +---------- gazza @ iconz.co.nz is Gary Elmes, Auckland, NZ --------+
- |"The reasonable man adapts himself to the World, the unreasonable one|
- |persists in trying to adapt the World to himself. Therefore all |
- |progress depends on unreasonable men." - George Bernard Shaw |
- +--------------- public PGP key available on request -----------------+
-